var pageNum;
$(function () {
    //初始化专业搜索条件
    $.initSubject({
        element:'subject-box',
        confirmBtn:false,
        removeBtn: false,
        style:false,
        notMultiple:true,
        classText:'form-control mb10',
        width:100,
        selectCallback:function(subjectIds){
            $("#subjectIds").val(subjectIds);
        }
    });

    //页面加载完成获取第一页数据
    getLoresetList(1);
    $("body").on('click','#btn1,#btn2',function () {
        //点击搜索查询从第一页开始查询
        getLoresetList(1);
    });

    //查看知识体系
    lookLoreset();

    $("body").on("click",".fa-unlock-alt",function () {//冻结
        var obj = $(this);
        var loresetIds = obj.parent("a").data("id");
        icModale(2,"确认要冻结该知识体系吗?",3,function (){
            updateLoresetStatus(loresetIds, 2);
        });
    }).on("click",".fa-history",function () {//恢复
        var obj = $(this);
        var loresetIds = obj.parent("a").data("id");
        icModale(2,"确认要恢复该知识体系吗?",3,function (){
            updateLoresetStatus(loresetIds, 1);
        });
    }).on("click","#lockBatch",function () {//批量冻结
        var obj = $('input[name=loresetIds]:checked');
        var loresetIds = obtainIds(obj);
        if(!$.isEmptyObject(loresetIds)){
            icModale(2,"确定要冻结选中知识体系吗?",3,function (){
                updateLoresetStatus(loresetIds, 2);
            });
        }else{
            icModale(0,"请选择要冻结的知识体系");
        }
    }).on("click","#regainBatch",function () {//批量恢复
        var obj = $('input[name=loresetIds]:checked');
        var loresetIds = obtainIds(obj);
        if(!$.isEmptyObject(loresetIds)){
            icModale(2,"确认要恢复选中的知识体系吗?",3,function (){
                updateLoresetStatus(loresetIds, 1);
            });
        }
    }).on("click",".fa-trash-o",function(){//单个删除
        var obj = $(this).parent('a');
        var loresetId = obj.data("id");
        icModale(2,"确认要删除该知识体系吗?",3,function (){
            updateLoresetStatus(loresetId, 3);
            icModale(0,'删除成功');
        });

    }).on("click","#deleteBatch",function () {//批量删除
        var obj = $("input[name=loresetIds]:checked");
        var loresetIds = obtainIds(obj);
        if(!$.isEmptyObject(loresetIds)){
            icModale(2,"确认要删除选中的知识体系吗?",3,function (){
                updateLoresetStatus(loresetIds, 3);
                icModale(0,'删除成功');
            });
        }
    });
    //初始化列表
    function getLoresetList(pageNo) {
        var params = $("#search-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/manage/loreset/ajax/ajaxQueryLoresetList',//请求URL
            contextId: 'loreset-list-context',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: getLoresetList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'select-all-loreset', //全选框ID
            callback: function (pageNo) {//数据加载完成回调
                pageNum = pageNo;
            }
        };
        ajaxGetListData(obj)
    }
});


//查看知识体系
function lookLoreset() {
    $("body").on("click",".fa-eye",function () {
        var loresetId = $(this).parents('a').data('id');
        var loresetName = $("#loreset-name-"+loresetId).text();
        var index = parent.layer.open({
            type: 2,
            shadeClose: false,
            shade: [0.3, '#000000'],
            title: '<strong>查看知识体系【'+ loresetName +'】</strong>',
            area: ['800px', '500px'],
            content: baseloaction + "/admin/manage/loreset/loresetInfo?id="+loresetId,
            btn: ['进入修改状态', '取消'],
            yes: function(index, layero) {
                window.location.href= baseloaction + '/admin/manage/loreset/initUpdate?id='+loresetId;
                parent.layer.close(index);
            }
        });
    });
}

//修改知识体系状态
function updateLoresetStatus(loresetIds, status) {
    $.ajax({
        url: baseloaction + "/admin/manage/loreset/updateLoresetStatus",
        data:{"loresetIds":loresetIds,"status":status},
        type:"post",
        dataType:"json",
        success:function (result) {
            if(result.success){
                var arr = (loresetIds+"").split(",");
                $.each(arr,function (index,id) {
                    var statusEm = $("#lore-status-"+id);
                    var actionEm = $("#lore-action-status-"+id);
                    if(1 == status){
                        statusEm.html('<span class="label label-info">正常</span>');
                        actionEm.attr('data-original-title','冻结');
                        actionEm.html('<i class="fa fa-unlock-alt"></i>');
                    }else if(2 == status){
                        statusEm.html('<span class="label label-danger">冻结</span>');
                        actionEm.attr('data-original-title','恢复');
                        actionEm.html('<i class="fa fa-history"></i>');
                    }else if(3 == status){
                        $("#loreset-"+id).remove();
                    }
                });
            }else{
                icModale(0,result.message);
            }
        },
        error:function (error) {
            icModale(0,"系统繁忙,请稍后重试");
        }
    });
}

//拼接id串
function obtainIds(obj) {
    if(null == obj || obj.size() == 0){
        icModale(0,"请选择要操作的数据");
        return null;
    }
    var loresetIds = "";
    $(obj).each(function (index,_input) {
        loresetIds += _input.value + ",";
    })
    loresetIds = loresetIds.substring(0,loresetIds.length-1);
    return loresetIds;
}